﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal.WebControls;

using zevenseas.LifeCycleManagement.Utilities;
using zevenseas.LifeCycleManagement.TimerJobs;

using Microsoft.SharePoint.WebPartPages;

namespace zevenseas.LifeCycleManagement.ApplicationPages
{
    public class ConfigTimerJobs : Microsoft.SharePoint.WebControls.LayoutsPageBase
    {
        private ErrorHandling errorHandling;

        protected string GatherLastRunTime= string.Empty;
        protected string ActionLastRunTime = string.Empty;

        protected Label output;

        protected WebApplicationSelector webApplicationSelector;
        protected Button btnOk;
        protected SchedulePicker SchedulerAction; 
        protected SchedulePicker SchedulerGather;

        public ConfigTimerJobs()
        {
            errorHandling = new ErrorHandling();
            errorHandling.productName = "ConfigTimerJobs";
            errorHandling.exeName = "zevenseas.LifeCycleManagement.ApplicationPages";
        }

        protected override void OnLoadComplete(EventArgs e)
        {
            SPWeb web = SPContext.Current.Web;
            SPWebApplication webApplication = webApplicationSelector.CurrentItem;
            
            try
            {
                if (!Page.IsPostBack)
                {
                    if (Utilities.Utilities.GetWebProperty("LCMTimerjobsRunning", web.Properties) == "true")
                    {
                        //Displays the current schedule and last run status
                        SiteLifeCycleGatherTimerjob gatherJob = webApplication.JobDefinitions.GetValue<SiteLifeCycleGatherTimerjob>("LCMGatherJob");
                        if (gatherJob != null)
                        {
                            GatherLastRunTime = "last ran at " + gatherJob.LastRunTime.ToShortDateString();
                        }

                        SiteLifeCycleActionTimerjob actionJob = webApplication.JobDefinitions.GetValue<SiteLifeCycleActionTimerjob>("LCMActionJob");
                        if (actionJob != null)
                        {
                            ActionLastRunTime = "last ran at " + actionJob.LastRunTime.ToShortDateString();
                        }
                        

                        foreach (SPJobDefinition job in webApplication.JobDefinitions)
                        {
                            if (job.Name == "LCMActionJob" || job.Name == "LCMGatherJob")
                            {
                                switch (job.Name)
                                {
                                    case "LCMActionJob":
                                        SchedulerAction.ScheduleString= job.Schedule.ToString();
                                        break;
                                    case "LCMGatherJob":
                                        SchedulerGather.ScheduleString = job.Schedule.ToString();
                                        break;
                                }
                            }
                        }       
                    }
                }
            }
            catch (Exception error)
            {
                output.Text += error.Message.ToString();
            }

            base.OnLoadComplete(e);
        }

        protected void OnClickOK(Object Sender, EventArgs e)
        {

            SPWeb web = SPContext.Current.Web;

            try
            {
                Utilities.Utilities.SetWebProperty("true", "Configured", web.Properties);
                InstallTimerJob();

            }
            catch
            {
                Utilities.Utilities.SetWebProperty("false", "Configured", web.Properties);
            }

            this.Page.Response.Redirect("/_admin/applications.aspx");
        }

        protected void OnClickCancel(Object Sender, EventArgs e)
        {
            this.Page.Response.Redirect("/_admin/applications.aspx");
            
        }

        protected void btn_InstallTimerJob(Object Sender, EventArgs e)
        {
            InstallTimerJob();

        }

        protected void btn_UnInstallTimerJob(Object Sender, EventArgs e)
        {
            UnInstallTimerJob();
        }           

        private void InstallTimerJob()
        {           
            //First remove the scheduled jobs
            UnInstallTimerJob();

            //Now we schedule the timer jobs
            SPWebApplication webApplication = webApplicationSelector.CurrentItem;
            try
            {
                SiteLifeCycleGatherTimerjob siteLifeCycleGatherJob = new SiteLifeCycleGatherTimerjob("LCMGatherJob", webApplication,null, SPJobLockType.Job);               
                siteLifeCycleGatherJob.Schedule = SchedulerGather.Schedule;                
                siteLifeCycleGatherJob.Update();

                SiteLifeCycleActionTimerjob siteLifeCycleActionTimerJob = new SiteLifeCycleActionTimerjob("LCMActionJob", webApplication, null, SPJobLockType.Job);                
                siteLifeCycleActionTimerJob.Schedule = SchedulerAction.Schedule;
                siteLifeCycleActionTimerJob.Update();

                SPWeb web = SPContext.Current.Web;
                Utilities.Utilities.SetWebProperty("true", "LCMTimerjobsRunning", web.Properties);                
            }
            catch (Exception error)
            {
                output.Text += "<BR>" + error.Message.ToString();
            }

        }

        private void UnInstallTimerJob()
        {
            SPWebApplication webApplication = webApplicationSelector.CurrentItem;
            foreach (SPJobDefinition job in webApplication.JobDefinitions)
            {
                if (job.Name == "LCMActionJob" || job.Name == "LCMGatherJob")
                {
                    job.Delete();
                }
            }            
        }
    }
}
